const webpack = require('webpack');
const path = require('path');
const {SourceMap} = require('module');
const resolve = (dir) => path.resolve(__dirname, dir);

module.exports = {
    webpack: {
        output: {
            publicPath: '/',
        },
        cache: {
            type: 'filesystem',
        },
        alias: {
            '@': resolve('src'),
        },
        optimization: {
            minimize: true,
        },
        plugins: [
            new webpack.ProgressPlugin((percentage, message, ...args) => {
                const progress = Math.round(percentage * 100);
                // ANSI 转义序列设置绿色字体
                const greenText = '\x1b[32m'; // 绿色字体
                const reset = '\x1b[0m'; // 重置颜色

                // 使用 '\r' 让进度信息在同一行更新
                const output = `${greenText}Progress: ${progress}% - ${message}${reset} ${args.join(' ')}`;
                process.stdout.write(`\r${output}`);
            }),
        ],
        configure: (webpackConfig, {env, paths}) => {
            // 可以在这里进一步调整 webpack 配置
            return webpackConfig;
        },
        mode: 'development',
        devtool: 'cheap-module-source-map',
        optimization: {
            usedExports: true,
        },
    },
    devServer: {
        compress: true,
        https: true,
        hot: true,
        proxy: {
            '/api': {
                target: 'http://rap2api.taobao.org/app/mock/289177',
                changeOrigin: true,
                pathRewrite: {
                    '^/api': '',
                },
            },
        },
        historyApiFallback: true, // ⭐ 添加这一行，解决前端路由问题
    },
};
